﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Iacym.Entities;
using Iacym.BusinessLogic;
using Microsoft.Practices.Unity;


namespace Iacym.UI
{
    public partial class FrmMttoCelula : Form
    {
        [Dependency]
        public ICelulaBL CelulaBL { get; set; }

        [Dependency]
        public IMinisterioBL MinisterioBL { get; set; }

        bool isNew;

        public FrmMttoCelula()
        {
            InitializeComponent();
            this.BuildUp();

        }

        public CELULA _celula { get; set; }
        public USUARIO currentUsuarioMttoCelula { get; set; }

        public void AddCelula()
        {
            isNew = true;
            cELULABindingSource.AddNew();
        }
        
        public void UpdateCelula()
        {
            
            cELULABindingSource.DataSource = _celula;
        }
        private void btnGuardar_Click(object sender, EventArgs e)
        {
            CELULA celula = cELULABindingSource.Current as CELULA;
            var msg = "ERROR";
            
            if (celula!=null)
            {
                if (isNew == true)
                {
                    try
                    {
                        celula.Descripcion = descripcionTextBox.Text.ToUpper();
                        CelulaBL.AddCelula(celula);
                        MessageBox.Show("Registro Correcto", "Mensaje", 
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {

                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                if(isNew == false)
                {
                    try
                    {
                        celula.Descripcion = descripcionTextBox.Text.ToUpper();
                        CelulaBL.UpdateCelula(celula);
                        MessageBox.Show("Modificación Correcta", "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {

                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje",
                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void descripcionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void descripcionTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }
              
        private void FrmMttoCelula_Load(object sender, EventArgs e)
        {
            
        }

        private void descripcionTextBox_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            var guion = Convert.ToString("-");

            string cadena = guion + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void descripcionTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                CargarMinisterio();
            }
        }

        private void BtnBusProc_Click(object sender, EventArgs e)
        {
            CargarMinisterio();
        }

        public void CargarMinisterio()
        {
            FrmMinisterio frm = new FrmMinisterio();
            frm.Extend();
            frm.currentUsuarioMinisterio = currentUsuarioMttoCelula;
            frm.ShowDialog();
           

            if (frm.CurrentMinisterio != null)
            {
                var nuevoMini = MinisterioBL.GetFromMinisterioById(frm.CurrentMinisterio.Id);
                var mini = cELULABindingSource.Current as CELULA;
                mini.MINISTERIO = nuevoMini;
                mini.MinisterioId = nuevoMini.Id;
                cELULABindingSource.ResetBindings(true);
            }

        }
    }
}
